package com.qf.day09.work2;

import java.util.Scanner;

public class Menu {
    private static int id = 0;
    private static Book newBook() {
        Scanner scanner = new Scanner(System.in);
        Book book = new Book();
        System.out.println("图书名称");
        String name = scanner.next();
        book.setBookName(name);
        System.out.println("图书价格");
        double price = scanner.nextDouble();
        book.setPrice(price);
        System.out.println("图书位置");
        String position = scanner.next();
        book.setPosition(position);
        book.setPid(id++);
        return book;
    }

    private static Book update() {
        Scanner scanner = new Scanner(System.in);
        Book book = new Book();
        System.out.println("修改后的图书名称");
        String name = scanner.next();
        book.setBookName(name);
        System.out.println("修改后的图书价格");
        double price = scanner.nextDouble();
        book.setPrice(price);
        System.out.println("修改后的图书位置");
        String position = scanner.next();
        book.setPosition(position);
        return book;
    }


    public void show() {
        Scanner scanner = new Scanner(System.in);
        Menu menu = new Menu();
        BookInterface book = new BookImpl();
        while (true){
            System.out.println("请选择 1.新增图书 2.删除图书 3.修改图书 4.查找所有图书 5.查找一本书  6.退出");
            String select =  scanner.next();

            switch (select){
                case "1": //新增图书
                    System.out.println("新增图书");
                    boolean addFlag =  book.addBook(newBook());
                    if(addFlag){
                        System.out.println("增加成功");
                    }else {
                        System.out.println("增加失败");
                    }
                    break;
                case "2":
                    System.out.println("删除图书的id");
                    String deId =scanner.next();
                    boolean deFlag = book.deleteBook(deId);
                    if(deFlag){
                        System.out.println("删除成功");
                    }else {
                        System.out.println("没有对应id的图书");
                    }
                    break;
                case "3":
                    System.out.println("修改图书");
                    boolean upFlag =  book.updateBook(update());
                    if(upFlag){
                        System.out.println("修改最后一本书成功");
                    }else {
                        System.out.println("没有书可以修改");
                    }
                    break;
                case "4":
                    System.out.println("查找所有图书");

                     Book[] arr = book.selectAll();
                     if (arr[0] == null){
                         System.out.println("没有图书");
                     }
                    for (Book book1 : arr) {
                        if(book1!=null){
                            System.out.println(book1);
                        }
                    }
                    break;
                case "5":
                    System.out.println("输入要查找的书id");
                    String findId = scanner.next();
                    Book selectOne = book.selectOne(findId);
                    if(selectOne  == null){
                        System.out.println("找不到");
                    }else {
                        System.out.println( selectOne);
                    }
                    break;
                case "6":
                    System.out.println("退出");
                    return;

            }
        }
    }
}
